package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdminDao;
import com.usian.admin.service.AdminService;
import com.usian.user.model.admin.dtos.ChannelDto;
import com.usian.user.model.admin.pojos.AdChannel;
import com.usian.user.model.common.dtos.PageResponseResult;
import com.usian.user.model.common.dtos.ResponseResult;
import com.usian.user.model.common.enums.AppHttpCodeEnum;

import org.springframework.stereotype.Service;

import java.util.Date;

    @Service
    public class AdminServiceImpl extends ServiceImpl<AdminDao, AdChannel>implements AdminService {
        @Override
        public ResponseResult findByNameAndPage(ChannelDto channelDto) {
            //判定channelDto是否为空
            if (channelDto==null){
                //如果DTO为null,则返回无效参数的状态信息
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            //2.分页参数检查
            channelDto.checkParam();
            //3.根据名称模糊分页查询
            LambdaQueryWrapper<AdChannel> adChannelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            //queryWrapper.like（AdChannel::getName,channel.getName()）;
            //不为null的时候增加条件
            adChannelLambdaQueryWrapper.like(channelDto.getName()!=null,AdChannel::getName,channelDto.getName());
            //分页查询
            Page pageInfo=new Page(channelDto.getPage(),channelDto.getSize());
            //调用分页方法
            IPage<AdChannel>page=page(pageInfo,adChannelLambdaQueryWrapper);
            //4.结果封装
            PageResponseResult pageResponseResult=new PageResponseResult(channelDto.getPage(),channelDto.getSize(),(int)page.getTotal());
            pageResponseResult.setData(page.getRecords());
            return pageResponseResult;
        }

        @Override
        public ResponseResult saveChannel(AdChannel channel) {
            //判定参数是否存在
            if (channel==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            //设置创建时间的参数
            channel.setCreatedTime(new Date());
            channel.setIsDefault(true);
            //保存
            save(channel);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        @Override
        public ResponseResult update(AdChannel channel) {
            //判定参数是否存在
            if (channel==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            updateById(channel);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        @Override
        public ResponseResult deleteById(Integer id) {
            //1.判定id是否存在
            if (id==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            //2.判定频道是否存在和状态是否在使用
            AdChannel channel = getById(id);
            if (channel==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            if (channel.getStatus()){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
            //3.执行删除
            removeById(id);
            //4.返回结果
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
    }